<?php
/*
 * datagrid of project tasks displayed asynchrounously in editproject.php
*/
include("../inc_files/utils/checksession.php");
// Create connection
include("../inc_files/utils/dbconnection.php");
//query for all customers
$projectID = $_GET["projectid"];
$query ="SELECT * FROM tasks WHERE Deleted != 1 AND Project = '".$projectID."' ORDER BY TaskID;";
$result = $mysqli->query( $query );

//count the number of returned results
$resultCount = $result->num_rows;


/*get the value (if any) of the id of the task
 * to be edited (if the user has selected the edit icon on a row)
*/
$editRowID = "none";
if (isset($_REQUEST['taskid']))
{
	if($_REQUEST['taskid'] == 'undefined' || empty($_REQUEST['taskid']))
	{
		$editRowID = "none";
	} else {

		$editRowID = $_REQUEST['taskid'];
	}
}

//if there are some results returned create a table showing the results
$rownumber = 0;
echo"<form name=\"tasksform\" id=\"tasksform\" action=\"#\"> ";
echo "<table class=\"datagrid\">
		<tr class=\"datagrid headerrow\">
		<th style=\"width:40%\">Title</th>
		<th style=\"width:15%\">Start</th>
		<th style=\"width:10%\">End</th>
		<th style=\"width:5%\">iCal</th>
		<th style=\"width:10%\">Budget</th>
		<th style=\"width:10%\">Progress</th>
		<th style=\"width:5%\"></th>
		<th style=\"width:5%\"></th>
		</tr>";
if( $resultCount > 0){
	while($row = mysqli_fetch_array($result))
	{
		if($rownumber %2 == 0 && $editRowID !=$row['TaskID'])
		{
			echo "<tr class=\"datagrid evenrow\">";
		}
		else if ($rownumber %2 != 0 && $editRowID !=$row['TaskID'])
		{
			echo "<tr class=\"datagrid oddrow\">";
		} else if ($editRowID ==$row['TaskID'])
		{
			echo "<tr class=\"datagrid shownewitem\">";
		}
			

		if($editRowID ==$row['TaskID']){
			if($row['Deleted'] == 2){ //deleting has been disabled
				{
					echo "<td style=\"width:40%\">" . $row['Title'] . "<input type=\"hidden\"  value=\"".$row['Title']."\"    name=\"title\" id=\"title\" ></td>";
					echo "<td style=\"width:15%\">" .date_format(date_create($row['StartDate']), 'd/m/Y'). "<input type=\"hidden\"  value=\"".date_format(date_create($row['StartDate']), 'd/m/Y')."\" name=\"startdate\" id=\"startdate\" ></td>";
					echo "<td style=\"width:15%\" colspan=\"2\">" .date_format(date_create($row['EndDate']), 'd/m/Y'). "<input type=\"hidden\"  value=\"".date_format(date_create($row['EndDate']), 'd/m/Y')."\"  name=\"enddate\" id=\"enddate\" ></td>";
				}
					
			} else {
				echo "<td style=\"width:40%\"><input type=\"text\" size=\"40\"  value=\"".$row['Title']."\"  name=\"title\" id=\"title\" width=\"100%\"></td>";
				echo "<td style=\"width:15%\"><input type=\"text\" size=\"10\"  value=\"".date_format(date_create($row['StartDate']), 'd/m/Y')."\"  name=\"startdate\" id=\"startdate\" onfocus='return raise_picker_ui(this, \"dd/mm/yyyy\")'></td>";
				echo "<td style=\"width:15%\" colspan=\"2\"><input type=\"text\" size=\"10\"  value=\"".date_format(date_create($row['EndDate']), 'd/m/Y')."\"  name=\"enddate\" id=\"enddate\" onfocus='return raise_picker_ui(this, \"dd/mm/yyyy\")'></td>";
			}
				
			echo "<td style=\"width:10%\"><input type=\"text\" size=\"5\"  value=\"".$row['Budget']."\"  name=\"budget\" id=\"budget\"><input type=\"hidden\" value=\"".$editRowID."\  name=\"taskid\"  id=\"taskid\"></td>";
			echo "<td style=\"width:10%\"><input type=\"text\" size=\"5\"  value=\"".$row['PercentComplete']."\"  name=\"percentcomplete\" id=\"percentcomplete\"><input type=\"hidden\" value=\"".$editRowID."\  name=\"taskid\"  id=\"taskid\"></td>";
			echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/apply_32.png\" alt=\"Submit\"  onclick=\"updatetask();\" style=\"cursor:pointer\"></td>";
			echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/cancel_32.png\" alt=\"Cancel changes\" onclick=\"refreshgrid()\" style=\"cursor:pointer\"></td>";



		} else {
			echo "<td style=\"width:40%\">" . $row['Title'] . "</td>";
			echo "<td style=\"width:15%\">" .date_format(date_create($row['StartDate']), 'd/m/Y'). "</td>";
			echo "<td style=\"width:10%\">" .date_format(date_create($row['EndDate']), 'd/m/Y'). "</td>";
			
			//generate URL for iCal
			$iCalUrl = "a_generateical.php?&nocache=".rand(0,10000)."&subject=Project: ".$projectID." Task: ".urlencode($row['Title'])."&duedate=".$row['EndDate'];
						
			echo "<td style=\"width:5%\"><a href=\"".$iCalUrl."\" style=\"background-color: transparent;\"><img src=\"../images/calendar_24.png\" width=\"24\" height=\"24\"  alt=\"Add this task to your calendar\" style=\"border: none;\" ></a></td>";
			echo "<td style=\"width:10%\">" . $row['Budget'] . "</td>";
			echo "<td style=\"width:10%\">" . $row['PercentComplete'] . "%</td>";
			if($row['Deleted'] == 2){ //deleting has been disabled
				echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/pencil_32.png\" alt=\"Edit records for ". $row['Title'] ."\" onclick=\"refreshgrid("  . $row['TaskID'] . ")\" style=\"cursor:pointer\"></td>";
				echo "<td style=\"width:5%\">&nbsp;</td>";
			} else {

				echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/pencil_32.png\" alt=\"Edit records for ". $row['Title'] ."\" onclick=\"refreshgrid("  . $row['TaskID'] . ")\" style=\"cursor:pointer\"></td>";
				echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/edit-delete-2-32.png\" alt=\"Delete records for ". $row['Title'] ."\" onclick=\"deletetask("  . $row['TaskID'] . ")\" style=\"cursor:pointer\"></td>";
			}
				
		}
		echo "</tr>";
		$rownumber++;
	}

}
if($editRowID =="none")	{
	//if the user is not editing a row in the table show the
	//add the new item row (hidden at first) and the add new row link
	echo "<tr id=\"newitemrow\" class=\"datagrid hidenewitem\">";
	echo "<td style=\"width:40%\"><input type=\"text\" size=\"40\" name=\"title\" id=\"title\" width=\"100%\"></td>";
	echo "<td style=\"width:15%\"><input type=\"text\" size=\"10\" name=\"startdate\" id=\"startdate\" onfocus='return raise_picker_ui(this, \"dd/mm/yyyy\")'></td>";
	echo "<td style=\"width:15%\"  colspan=\"2\"><input type=\"text\" size=\"10\" name=\"enddate\" id=\"enddate\" onfocus='return raise_picker_ui(this, \"dd/mm/yyyy\")'></td>";
	echo "<td style=\"width:10%\"><input type=\"text\" size=\"5\" name=\"budget\" id=\"budget\"><input type=\"hidden\" value=\"new\"  name=\"taskid\"  id=\"taskid\" ></td>";
	echo "<td style=\"width:10%\"><input type=\"text\" size=\"5\" name=\"percentcomplete\" id=\"percentcomplete\"><input type=\"hidden\" value=\"new\"  name=\"taskid\"  id=\"taskid\" ></td>";
	echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/apply_32.png\" alt=\"Submit\"  onclick=\"updatetask();\" style=\"cursor:pointer\"></td>";
	echo "<td style=\"width:5%\"><img height=\"32\" width=\"32\"  src=\"../images/cancel_32.png\" alt=\"Cancel\"  onclick=\"togglenewitem();\" style=\"cursor:pointer\"></td>";
	echo "</tr>";

	//add the footer


	echo "<tr class=\"datagrid footer\">";
	echo "<td colspan=\"6\" style=\"width:30%\">
			<a href=\"javascript:;\" onclick='togglenewitem();'>Add new task
			<img src=\"../images/add_32.png\" style=\"border:none\" height=\"32\" width=\"32\" alt=\" Add new task row.\" ></a></td>";

	echo "</td>
			<td colspan=\"2\" style=\"width:30%\"></td>
			</tr>";
} else {
	//if the user is editing a row don't show the add new row link
	echo "<tr class=\"datagrid footer\">
			<td colspan=\"8\" ></td></tr>";
}
echo "</table>";
echo "</form>";
//tidy up database connection
$mysqli->close();



?>